#include "stdafx.h"
#include "wf_modifymove.h"

#include "pf_snapper.h"



WF_ModifyMove::WF_ModifyMove(PF_Container& container,
        PF_GraphicView& graphicView)
        :PF_PreviewActionInterface("Move Entities",
                           container, graphicView) {}


QAction* WF_ModifyMove::createGUIAction(LX_Define::ActionType /*type*/, QObject* /*parent*/) {
	// tr("&Move / Copy")
    QAction* action = new QAction(tr("&Move / Copy"),  NULL);
	action->setIcon(QIcon(":/extui/modifymove.png"));
    //action->zetStatusTip(tr("Move or copy entities one or multiple times"));
    return action;
}

void WF_ModifyMove::init(int status) {
    PF_ActionInterface::init(status);
}



void WF_ModifyMove::trigger() {

    PF_DEBUG->print("PF_ActionModifyMove::trigger()");

    PF_Modification m(*container, graphicView);
    m.move(data);

    PF_DIALOGFACTORY->updateSelectionWidget(container->countSelected());
}



void WF_ModifyMove::mouseMoveEvent(PF_MouseEvent* e) {
    PF_DEBUG->print("PF_ActionModifyMove::mouseMoveEvent begin");

    if (getStatus()==SetReferencePoint ||
            getStatus()==SetTargetPoint) {

        PF_Vector mouse = snapPoint(e);
        switch (getStatus()) {
        case SetReferencePoint:
            referencePoint = mouse;
            break;

        case SetTargetPoint:
            if (referencePoint.valid) {
                targetPoint = mouse;

                deletePreview();
                preview->addSelectionFrom(*container);
                preview->move(targetPoint-referencePoint);
                drawPreview();
            }
            break;

        default:
            break;
        }
    }

    PF_DEBUG->print("PF_ActionModifyMove::mouseMoveEvent end");
}



void WF_ModifyMove::mouseReleaseEvent(PF_MouseEvent* e) {
    if (e->button()==Qt::LeftButton) {
        PF_CoordinateEvent ce(snapPoint(e));
        coordinateEvent(&ce);
    } else if (e->button()==Qt::RightButton) {
        deletePreview();
        init(getStatus()-1);
    }
}

void WF_ModifyMove::coordinateEvent(PF_CoordinateEvent* e) {

    if (e==NULL) {
        return;
    }

    PF_Vector pos = e->getCoordinate();

    switch (getStatus()) {
    case SetReferencePoint:
        referencePoint = pos;
        graphicView->moveRelativeZero(referencePoint);
        setStatus(SetTargetPoint);
        break;

    case SetTargetPoint:
        targetPoint = pos;
        graphicView->moveRelativeZero(targetPoint);
        setStatus(ShowDialog);
        if (PF_DIALOGFACTORY->requestMoveDialog(data)) {
            data.offset = targetPoint - referencePoint;
            trigger();
            finish();
        }
        break;

    default:
        break;
    }
}


void WF_ModifyMove::updateMouseButtonHints() {
    switch (getStatus()) {
        /*case Select:
            PF_DIALOGFACTORY->updateMouseWidget(tr("Pick entities to move"),
                                           tr("Cancel"));
            break;*/
    case SetReferencePoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify reference point"),
                                            tr("Cancel"));
        break;
    case SetTargetPoint:
        PF_DIALOGFACTORY->updateMouseWidget(tr("Specify target point"),
                                            tr("Back"));
        break;
    default:
        PF_DIALOGFACTORY->updateMouseWidget("", "");
        break;
    }
}



void WF_ModifyMove::updateMouseCursor() {
    graphicView->setMouseCursor(LX_Define::CadCursor);
}



void WF_ModifyMove::updateToolBar() {
    switch (getStatus()) {
        /*case Select:
            PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSelect);
            break;*/
    case SetReferencePoint:
    case SetTargetPoint:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarSnap);
        break;
    default:
        PF_DIALOGFACTORY->requestToolBar(LX_Define::ToolBarModify);
        break;
    }
}


// EOF
